﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;
using StudentManage.Common;

namespace StudentManage.Web.Admin.Management.SysManage
{
    public partial class UsersList : SecurityPage
    {
        public override string PrivCode()
        {
            return "1f43b57ba11d7b63";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            DropBing();
            RepDataBing();
        }

        int i = 0;
        public string GetXN()
        {
            if (i == 0)
                i = (this.AspNetPager1.PageSize) * (this.AspNetPager1.CurrentPageIndex - 1);

            i += 1;
            return i.ToString();
        }

        private void DropBing()
        {
            List<Role> Rolelist = Role.All().ToList();
            this.Drop_Role.DataSource = Rolelist;
            this.Drop_Role.DataValueField = "RoleCode";
            this.Drop_Role.DataTextField = "RoleName";
            this.Drop_Role.DataBind();

            List<Role> SearchRolelist = Role.All().ToList();
            SearchRolelist.Add(new Role() { RoleCode="0", RoleName="全部"});
            this.Drop_SearchRole.DataSource = SearchRolelist.OrderBy(p => p.RoleCode).ToList();
            this.Drop_SearchRole.DataValueField = "RoleCode";
            this.Drop_SearchRole.DataTextField = "RoleName";
            this.Drop_SearchRole.DataBind();

            this.Drop_DataRole.Items.Clear();
            foreach (int indexNumber in Enum.GetValues(typeof(EnumData.DataRole)))
            {
                this.Drop_DataRole.Items.Add(
                    new ListItem()
                    {
                        Text = Enum.GetName(typeof(EnumData.DataRole), indexNumber),
                        Value = indexNumber.ToString()
                    });
            }
        }

        private bool IsExistsUserName(string NewUserName,int UserId)
        {
            if (UserId != 0)
            {
                return UserList.Where("UserName=@0 and ID!=@1", NewUserName, UserId).Count() > 0;
            }
            else
            {
                return UserList.Where("UserName=@0", NewUserName).Count() > 0;
            }
        }

        private void RepDataBing()
        {
            var eps = DynamicLinqExpressions.True<UserList>();
            eps = eps.And(p =>p.XH.Contains(this.txt_SearchHX.Text.Trim()));
            eps = eps.And(p => p.UName.Contains(this.txt_SearchName.Text.Trim()));
            if (this.Drop_SearchRole.SelectedValue != "0")
            {
                eps = eps.And(p => p.RoleCode == this.Drop_SearchRole.SelectedValue);
            }

            int RowCount = LinqUtility.LinqContext.UserList.Where(eps).Count();
            this.AspNetPager1.RecordCount = RowCount;
            int PageSize = this.AspNetPager1.PageSize;
            int SkipCount = PageSize * (this.AspNetPager1.CurrentPageIndex - 1);

            List<UserList> newslist = LinqUtility.LinqContext.UserList.Where(eps).OrderByDescending(p => p.AddTime).Skip(SkipCount).Take(PageSize).ToList();
            this.RepList.DataSource = newslist;
            this.RepList.DataBind();
        }

        protected void btPost_Click(object sender, EventArgs e)
        {
            int Userid = 0;
            UserList md;
            if (this.HidIsEdit.Value == "0")
            {
                md = new UserList();
            }
            else
            { 
                Userid = Convert.ToInt32(this.HidUserId.Value);
                md = UserList.Where("id=@0", Userid).First();
            }
            md.XH = this.txt_XH.Text.Trim();
            md.IsStudent=(this.Drop_IsStudent.SelectedValue=="0");
            if (this.IsExistsUserName(this.txt_UserName.Text.Trim(), Convert.ToInt32(this.HidUserId.Value)))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>$(document).ready(function () {alert('存在相同用户名！');});</script>");
                return;
            }
            md.UserName = this.txt_UserName.Text.Trim();
            
            if (this.HidIsEdit.Value == "0")
            {
                md.PassWord = Utility.GetMD5PassWord(this.txt_PassWord.Text.Trim(), "");
            }
            else
            {
                md.PassWord = (this.txt_PassWord.Text.Trim() != md.PassWord) ? Utility.GetMD5PassWord(this.txt_PassWord.Text.Trim(), "") : md.PassWord;
            }
            md.UName = this.txt_UName.Text.Trim();
            md.Tel = this.txt_Tel.Text.Trim();
            md.QQ = this.txt_QQ.Text.Trim();
            md.Email = this.txt_Email.Text.Trim();
            md.RoleCode = this.Drop_Role.SelectedValue;
            md.RoleName = this.Drop_Role.SelectedItem.Text;
            md.DataRole = Convert.ToInt32(this.Drop_DataRole.SelectedValue);
            md.AddTime = DateTime.Now;
            if (this.HidIsEdit.Value == "0")
            {
                UserList.Insert(md);
            }
            else
            {
                UserList.Update(md, "id=@0", Userid);
            }

            RoleUsers RU = RoleUsers.Where("UserCode=@0", md.UserName).FirstOrDefault();
            if (RU != null)
            {
                RU.RoleCode = this.Drop_Role.SelectedValue;
                RU.UserName = this.txt_UName.Text.Trim();
                RoleUsers.Update(RU, "id=@0", RU.id);
            }
            else
            {
                RoleUsers Rmd = new RoleUsers();
                Rmd.UserCode = md.UserName;
                Rmd.RoleCode = this.Drop_Role.SelectedValue;
                Rmd.UserName = this.txt_UName.Text.Trim();
                RoleUsers.Insert(Rmd);
            }

            RepDataBing();
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>$(document).ready(function () {alert('修改成功！');});</script>");
        }

        protected void RepList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int Userid = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "Del")
            {
                UserList.Delete("id=@0", Userid);
                this.RepDataBing();
            }
            else
            {
                this.HidIsEdit.Value = "1";
                this.HidUserId.Value = Userid.ToString();

                UserList md = UserList.Where("id=@0", Userid).First();
                this.txt_XH.Text = md.XH;
                this.Drop_IsStudent.SelectedValue = (Convert.ToBoolean(md.IsStudent) ? "0" : "1");
                this.txt_UserName.Text = md.UserName;
                this.txt_PassWord.Text = md.PassWord;
                this.txt_PassWord.Attributes.Add("value", md.PassWord);
                this.txt_UName.Text = md.UName;
                this.txt_Tel.Text = md.Tel;
                this.txt_QQ.Text = md.QQ;
                this.txt_Email.Text = md.Email;
                this.Drop_Role.SelectedValue = md.RoleCode;
                this.Drop_DataRole.SelectedValue = md.DataRole.ToString();
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>$(document).ready(function () {Showdlg() });</script>");
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            this.RepDataBing();
        }

        protected void Btn_Search_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            this.RepDataBing();
        }
    }
}